import { Button, Modal, message } from 'antd';
import { action, observable, toJS } from "mobx";
import * as React from 'react';
import DialogStore from ".";
import { WeaLocaleProvider } from "ecCom";
import API from '../../apis/task';
const getLabel = WeaLocaleProvider.getLabel;
import { isEqual } from "lodash";

export default class AddIsSueStore extends DialogStore {

    @observable datas = [];
    @observable columns = [];

    constructor() {
        super();
        this.buttons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@g7l5et@ok`} onClick={this.onSave} type="primary">{getLabel('506028', '确定')}</Button>,
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pxd344@cancel`} onClick={this.hide} type="primary">{getLabel('506972', '取消')}</Button>];
    }

    getColumns = () => {
        return [{
            com: [
                {
                    type: "BROWSER",
                    key: "id",
                    browserConditionParam: {
                        completeParams: {},
                        conditionDataParams: {},
                        dataParams: this.dataParams || {},
                        destDataParams: this.dataParams || {},
                        hasAddBtn: false,
                        hasAdvanceSerach: true,
                        idSeparator: ",",
                        isAutoComplete: 1,
                        isDetail: 0,
                        isMultCheckbox: false,
                        isSingle: true,
                        pageSize: 10,
                        quickSearchName: "",
                        replaceDatas: [],
                        title: getLabel('502308', '浏览框'),
                        type: 1,
                        viewAttr: 1,
                        ...this.tabs
                    }
                }
            ],
            dataIndex: 'browser',
            title: getLabel('509237', '人员'),
            width: '50%',
            colSpan: 1,
        },
        {
            com: [
                {
                    style: { width: 230 }, multiple: true, type: 'SELECT', key: 'sheetIds', options: this.options, viewAttr: 3
                }
            ],
            dataIndex: 'select',
            title: getLabel('515243', 'sheet页'),
            width: '50%',
            colSpan: 1,
        }]
    }

    @action
    initOptions = (params) => {
        //根据pageid获取Pagesheet
        API.getEdcPageSheet(params).then((result) => {
            this.options = result.map(r => ({ key: r.key, showname: r.showname }));
            this.columns = this.getColumns();
        });
    }

    @action
    willAdd = (datas, addData) => {
        if (this.browser) {
            this.browser.openModal();
        }
    };

    @action
    changeBrowser = (ids, names, datas) => {
        let idArr = this.options.map(o => o.key) || [];
        this.datas = datas.map((data) => {
            const existsData = this.datas.find(d => d.id === data.id);
            if (existsData) {
                return existsData;
            } else {
                return { id: data.id, idspan: data.name, sheetIds: idArr.join(',') };
            }
        });
    }

    @action
    onSave = () => {
        let canSave = true;
        //获取当前page页的所有sheetid
        let idArr = this.options.map(o => o.key);
        let datas = toJS(this.datas);
        datas = datas.map(data => {
            const { sheetIds } = data;
            if (!sheetIds) {
                canSave = false;
            }
            return data;
        });
        if (canSave) {
            if (datas.length == 0) {
                message.error(getLabel('515326', '没有添加记录！'));
            } else {
                this.parent().issue(JSON.stringify(datas));
            }
        } else {
            message.error(getLabel('515242', 'sheet页为空！'));
        }
    }

    @action
    onChange = (datas) => {
        this.datas = datas;
    }

    @action
    issue = (ids) => {
        let idArr = ids.split(',') || [];
        idArr = idArr.map(id => {
            return { id, sheetIds: '0' };
        });
        this.parent().issue(JSON.stringify(idArr));
    }
}